{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray的数据类型",
   "id": "9774c983afccadcf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T00:48:54.372825Z",
     "start_time": "2025-07-30T00:48:54.350174Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "布尔类型 bool\n",
    "整数类型 int unit\n",
    "浮点数 float\n",
    "复数 complex\n",
    "\"\"\""
   ],
   "id": "5ea3178b0d4e24bf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n布尔类型 bool\\n整数类型 int unit\\n浮点数 float\\n复数 complex\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T00:58:36.363396Z",
     "start_time": "2025-07-30T00:58:36.167305Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "30e433d080f2bbc9",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-28T11:41:42.829869Z",
     "start_time": "2025-07-28T11:41:42.818899Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 0, 1, -2], dtype=np.bool)\n",
    "print(arr)"
   ],
   "id": "abe1959e048c5fe7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False  True  True]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-28T14:06:04.610694Z",
     "start_time": "2025-07-28T14:06:04.591308Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组的索引和切片\n",
    "arr = np.random.randint(1, 100, 10)\n",
    "print(arr)\n",
    "print(arr[0]) # 打印arr中的第1个元素\n",
    "print(arr[0:2]) # 打印arr中的前2 个元素\n",
    "print(arr[slice(0,2)]) # 打印arr中的前2 个元素\n",
    "print(arr[arr < 50]) # 基于布尔条件进行筛选数组\n",
    "print(arr[(arr > 20) & (arr < 50)]) # 基于布尔条件进行筛选数组"
   ],
   "id": "6a0cd6c7c425945e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[98 37 25 30  4 20 43 70 10 58]\n",
      "98\n",
      "[98 37]\n",
      "[98 37]\n",
      "[37 25 30  4 20 43 10]\n",
      "[37 25 30 43]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-28T14:25:42.276563Z",
     "start_time": "2025-07-28T14:25:42.254481Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维矩阵的索引和切片\n",
    "arr = np.random.randint(1, 100, (4, 5))\n",
    "print(arr)\n",
    "print(arr[1, 3]) # 获取第2行第4列的元素：单个元素\n",
    "print(arr[0:3, 0:3]) # 获取子矩阵\n",
    "print(arr[0][ arr[0] > 5 ]) # 一层一层的嵌套\n",
    "print(arr[:, 1]) # 获取第2列的数据, ':'表示获取所有行的数据"
   ],
   "id": "ac265dfe26e18ba4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[66  6 24  5 17]\n",
      " [27 57  1 25 49]\n",
      " [48 27 70 49 60]\n",
      " [52 53 76 78 54]]\n",
      "25\n",
      "[[66  6 24]\n",
      " [27 57  1]\n",
      " [48 27 70]]\n",
      "[66  6 24 17]\n",
      "[ 6 57 27 53]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T00:58:31.068610Z",
     "start_time": "2025-07-29T00:58:31.058637Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组的算术运算\n",
    "a = np.array([1, 2, 3])\n",
    "b = np.array([1, 2, 3])\n",
    "print((a + b))\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)"
   ],
   "id": "b7e9c0da94a3671d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4 6]\n",
      "[0 0 0]\n",
      "[1 4 9]\n",
      "[1. 1. 1.]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:01:51.341155Z",
     "start_time": "2025-07-29T01:01:51.332873Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "f64de8ce9f525058",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T00:58:42.036183Z",
     "start_time": "2025-07-30T00:58:42.016238Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2维数组的算术运算\n",
    "a = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])\n",
    "b = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])\n",
    "print((a + b))\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)"
   ],
   "id": "55ae983faee01d9f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4 6]\n",
      " [2 4 6]\n",
      " [2 4 6]]\n",
      "[[0 0 0]\n",
      " [0 0 0]\n",
      " [0 0 0]]\n",
      "[[1 4 9]\n",
      " [1 4 9]\n",
      " [1 4 9]]\n",
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:07:10.329750Z",
     "start_time": "2025-07-29T01:07:10.310996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播运算\n",
    "arr = np.array([1, 2, 3])\n",
    "print(arr + 2) # 当数组中的元素无法对应时，会自动进行广播运算，等价于 [1+2, 2+2, 3+2] "
   ],
   "id": "da5c1a72e882e7e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T02:14:48.848396Z",
     "start_time": "2025-07-29T02:14:48.836628Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 维度对齐\n",
    "arr = np.array([1, 2, 3])\n",
    "print(f'一维广播：{arr + 1}') # 等价于np.array([1, 2, 3]) + np.array([1, 1, 1])\n",
    "arr2 = np.array([1, 2, 3]) # 形状为(3,)\n",
    "arr3 = np.array([[1, 2, 3],[4, 5, 6], [7 , 8, 9]]) # 形状维3*3\n",
    "\"\"\"\n",
    "arr2 + arr3 的广播过程\n",
    "1. 维度对齐(若2个数组的维度不同，维度较少的数组会从前面补1, 直至维度相同为止)\n",
    "[1, 2, 3] -> [[1, 2, 3]]  shape：(3,) -> (1, 3)\n",
    "\n",
    "2. 形状对齐校验(从尾部维度开始比较：两个维度要么相等，要么其中一个是1，否则就报错)\n",
    "(1, 3) 与 (3, 3) 进行形状对齐，3与3相等，1与3不等，但是其中一个是1\n",
    "\n",
    "3. 形状对齐(当某个维度为1时，NumPy 会在该维度上复制数据以匹配另一个维度)\n",
    "[[1, 2, 3]] -> [\n",
    "                [1, 2, 3], \n",
    "                [1, 2, 3],\n",
    "                [1, 2, 3]\n",
    "               ]\n",
    "shape: (3,) -> (3, 3)\n",
    "\n",
    "4. 2个数组的维度和形状均以对齐\n",
    "进行运算\n",
    "[\n",
    "    [1, 2, 3], \n",
    "    [1, 2, 3],      +    [[1, 2, 3],[4, 5, 6], [7 , 8, 9]\n",
    "    [1, 2, 3]\n",
    "]\n",
    "\"\"\"\n",
    "print(arr2 + arr3)"
   ],
   "id": "fe0cb4290529f410",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一维广播：[2 3 4]\n",
      "[[ 2  4  6]\n",
      " [ 5  7  9]\n",
      " [ 8 10 12]]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T02:27:56.595970Z",
     "start_time": "2025-07-29T02:27:56.575649Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播失败\n",
    "arr = np.array([[1, 2, 3], [1, 2, 3]]) # shape: (2, 3)\n",
    "arr2 = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) # shape: (3, 3)\n",
    "print(arr + arr2) # because  arr.shape[0] != arr2.shape[0]"
   ],
   "id": "475825e9272ea10b",
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (2,3) (3,3) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[20], line 4\u001B[0m\n\u001B[0;32m      2\u001B[0m arr \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([[\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m3\u001B[39m], [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m3\u001B[39m]]) \u001B[38;5;66;03m# shape: (2, 3)\u001B[39;00m\n\u001B[0;32m      3\u001B[0m arr2 \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marray([[\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m], [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m], [\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m]]) \u001B[38;5;66;03m# shape: (3, 3)\u001B[39;00m\n\u001B[1;32m----> 4\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43marr\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[43m \u001B[49m\u001B[43marr2\u001B[49m)\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (2,3) (3,3) "
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T02:44:13.585513Z",
     "start_time": "2025-07-29T02:44:13.567561Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 矩阵运算\n",
    "arr = np.array([[1, 2, 3],[4, 5, 6], [7 , 8, 9]])\n",
    "arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "np.dot(arr, arr2)\n",
    "np.dot(arr, arr2)"
   ],
   "id": "7de5d85220feb866",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 30,  36,  42],\n",
       "       [ 66,  81,  96],\n",
       "       [102, 126, 150]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T02:56:53.759498Z",
     "start_time": "2025-07-29T02:56:53.738181Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算平方根\n",
    "(np.sqrt(np.array([1, 4, 9])))"
   ],
   "id": "90574fd142753fdc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3.])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T02:58:42.808817Z",
     "start_time": "2025-07-29T02:58:42.800838Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算指数\n",
    "np.exp(1)"
   ],
   "id": "dba196fbd9e3d639",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(2.718281828459045)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:00:02.084650Z",
     "start_time": "2025-07-29T03:00:02.076944Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算自然对数\n",
    "np.log(2.718)"
   ],
   "id": "3ce7033cc4634fd6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.999896315728952)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:03:57.321179Z",
     "start_time": "2025-07-29T03:03:57.300237Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算三角函数\n",
    "np.sin(np.pi / 2)\n",
    "np.cos(np.pi / 2)"
   ],
   "id": "44c5e2b000ccd49c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(6.123233995736766e-17)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:06:49.186510Z",
     "start_time": "2025-07-29T03:06:49.167318Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 指数函数\n",
    "arr = np.array([1, 2, 3])\n",
    "np.power(arr, 3)"
   ],
   "id": "42799f53d9ae3f58",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  8, 27])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:08:52.368825Z",
     "start_time": "2025-07-29T03:08:52.350874Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 四舍五入\n",
    "np.round([1.4, 5.5, 1.6])"
   ],
   "id": "af3b8332f5ae4a7c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 6., 2.])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:11:53.017146Z",
     "start_time": "2025-07-29T03:11:53.008171Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 向上取整， 向下取整\n",
    "arr = np.array([1.3, 2.5, 5.4])\n",
    "print(np.ceil(arr)) # ceil: 天花板，向上取整\n",
    "print(np.floor(arr)) # floor: 地板，向下取整"
   ],
   "id": "272c5386e8da2330",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 3. 6.]\n",
      "[1. 2. 5.]\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:13:50.391961Z",
     "start_time": "2025-07-29T03:13:50.372124Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检测缺失值\n",
    "np.isnan([1, 2, np.nan, 3])"
   ],
   "id": "91ab61d9fb6cf8cf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:19:58.970978Z",
     "start_time": "2025-07-29T03:19:58.957835Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计函数\n",
    "np.sum([1, 2 ,3])"
   ],
   "id": "8845254d3b33bdd3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(6)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:20:09.299180Z",
     "start_time": "2025-07-29T03:20:09.279238Z"
    }
   },
   "cell_type": "code",
   "source": "np.mean([1, 2, 3])",
   "id": "72948bb000cf399d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(2.0)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:20:30.428260Z",
     "start_time": "2025-07-29T03:20:30.412303Z"
    }
   },
   "cell_type": "code",
   "source": "np.median([1, 2 ,3, 4])",
   "id": "ff4ce0d8a77413a6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(2.5)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T03:30:45.266506Z",
     "start_time": "2025-07-29T03:30:45.258529Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 分位数: 表示数据中有多少比例的值小于或等于该分位数值。例\n",
    "# TODO 待定，没太搞懂分位数的定义\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "np.percentile(arr, 20)"
   ],
   "id": "996665b81fd030c6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(1.8)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T04:05:38.476755Z",
     "start_time": "2025-07-29T04:05:38.459818Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 累计和\n",
    "arr = np.array([1, 2, 3, 4])\n",
    "np.cumsum(arr)\n",
    "# 新数组的 第i个元素 等于 原数组的 前i个元素的和"
   ],
   "id": "7c7193c17b8cef61",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  6, 10])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T04:07:25.476884Z",
     "start_time": "2025-07-29T04:07:25.460784Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 累积乘\n",
    "arr = np.array([1, 2, 3, 4])\n",
    "# 新数组的 第i个元素 等于 原数组的 前i个元素的连乘\n",
    "\n",
    "np.cumprod(arr)"
   ],
   "id": "b675283fa5638c2e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  6, 24])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:56:16.816595Z",
     "start_time": "2025-07-29T06:56:16.806210Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 比较函数\n",
    "# 数组中的每个元素是否大于 某个数\n",
    "print(np.greater([1, 2, 3, 4], 1))\n",
    "# 数组中的每个元素是否小于 某个数\n",
    "print(np.less([1, 2, 3, 4], 1))\n",
    "print(np.equal([1, 2, 3, 4], 1))\n",
    "\n",
    "np.array([1, 2, 0, 0], dtype=np.bool)\n",
    "\n",
    "print(np.logical_and([1, 1], [1, 0]))\n",
    "# 检查元素是否至少有一个元素为True\n",
    "print(np.any([1, 2, 0]))\n",
    "\n",
    "# 检查全部元素是否为True\n",
    "print(np.all([1, 1, 3, 4]))\n"
   ],
   "id": "f2bc00fa1a4fe00",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True  True  True]\n",
      "[False False False False]\n",
      "[ True False False False]\n",
      "[ True False]\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:57:58.978799Z",
     "start_time": "2025-07-29T06:57:58.964293Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 自定义条件\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "print(np.where(arr < 2,arr, -1))"
   ],
   "id": "b20f845ed2b93076",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1 -1 -1 -1 -1]\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T07:09:13.617187Z",
     "start_time": "2025-07-29T07:09:13.605119Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# \n",
    "scores = np.random.randint(50, 100, 20)\n",
    "print(scores)\n",
    "print(np.where(scores >= 60, '及格', '不及格'))"
   ],
   "id": "25fc6b04566899e5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[97 75 97 93 77 68 61 99 94 66 66 77 75 56 88 67 67 64 55 63]\n",
      "['及格' '及格' '及格' '及格' '及格' '及格' '及格' '及格' '及格' '及格' '及格' '及格' '及格' '不及格'\n",
      " '及格' '及格' '及格' '及格' '不及格' '及格']\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T07:05:12.329225Z",
     "start_time": "2025-07-29T07:05:12.314268Z"
    }
   },
   "cell_type": "code",
   "source": [
    "np.where(scores < 60, '不及格', \n",
    "         np.where(scores < 80, '良好', '优秀')\n",
    "         )\n"
   ],
   "id": "e94c1df7d5faae17",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['不及格', '不及格', '良好', '良好', '优秀', '良好', '良好', '优秀', '优秀', '优秀', '优秀',\n",
       "       '优秀', '优秀', '良好', '良好', '不及格', '不及格', '优秀', '良好', '不及格'],\n",
       "      dtype='<U3')"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 78
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T07:11:16.132001Z",
     "start_time": "2025-07-29T07:11:16.111512Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.select(condition, 返回的结果)\n",
    "# select结构清晰，可维护性高，代码可读性该  \n",
    "scores = np.random.randint(50, 100, 20)\n",
    "print(scores)\n",
    "print(np.select([scores<60,(scores >=60) & (scores<=80), scores>80],\n",
    "                ['不及格', '良好', '优秀'],\n",
    "                default='未知的'\n",
    "                ))"
   ],
   "id": "de68c0210a147669",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[67 74 84 83 81 60 80 70 60 63 96 73 76 69 96 92 90 75 80 92]\n",
      "['良好' '良好' '优秀' '优秀' '优秀' '良好' '良好' '良好' '良好' '良好' '优秀' '良好' '良好' '良好'\n",
      " '优秀' '优秀' '优秀' '良好' '良好' '优秀']\n"
     ]
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T12:59:30.513246Z",
     "start_time": "2025-07-29T12:59:30.503515Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 排序函数\n",
    "# 不生成新数组\n",
    "arr = np.random.randint(1, 100, 20) # 生成1~100范围的20个随机数\n",
    "print(arr)\n",
    "arr.sort() # 注意！！！ arr.sort()会重排数组中的元素，不会生成一个新数组\n",
    "print(arr)"
   ],
   "id": "d89c12eb2d784f54",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[28 67 40 99 70 42 91 65 41 69 88 24 70 83 70 43 22 10 29 47]\n",
      "[10 22 24 28 29 40 41 42 43 47 65 67 69 70 70 70 83 88 91 99]\n"
     ]
    }
   ],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T13:04:47.357435Z",
     "start_time": "2025-07-29T13:04:47.349864Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 排序函数\n",
    "# 生成新数组\n",
    "arr = np.random.randint(1, 100, 20) # 生成1~100范围的20个随机数 \n",
    "print(arr)\n",
    "# 注意！！！ np.sort 生成一个新数组\n",
    "print(np.sort(arr))\n",
    "print(arr)\n",
    "print(np.argsort(arr)) # argsort 获取排序后元素的索引数组\n",
    "print(arr)"
   ],
   "id": "d683e606fd6dcc97",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1 56 35 26 16 75 72  7 90 22 53 88 46 88  4 34 83 90 13 24]\n",
      "[ 1  4  7 13 16 22 24 26 34 35 46 53 56 72 75 83 88 88 90 90]\n",
      "[ 1 56 35 26 16 75 72  7 90 22 53 88 46 88  4 34 83 90 13 24]\n",
      "[ 0 14  7 18  4  9 19  3 15  2 12 10  1  6  5 16 11 13  8 17]\n",
      "[ 1 56 35 26 16 75 72  7 90 22 53 88 46 88  4 34 83 90 13 24]\n"
     ]
    }
   ],
   "execution_count": 92
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T13:06:23.837993Z",
     "start_time": "2025-07-29T13:06:23.821637Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 去重函数\n",
    "arr = np.random.randint(1, 10, 20) # \n",
    "print(arr)\n",
    "print(np.unique(arr))"
   ],
   "id": "8cb89dd50c825500",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 5 3 8 3 8 4 6 8 8 7 9 2 1 9 3 2 2 1 7]\n",
      "[1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T13:11:40.856713Z",
     "start_time": "2025-07-29T13:11:40.831781Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的拼接\n",
    "arr1 = np.array([1, 2])\n",
    "arr2 = np.array([3, 4])\n",
    "np.concatenate([arr1, arr2]) # np.concatenate() 要求传入一个 元组或列表 包含要连接的数组，而不是直接传入多个数组参数"
   ],
   "id": "4a480132628fa6bc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 100
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T13:44:32.401930Z",
     "start_time": "2025-07-29T13:44:32.383979Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1, 10, 10)\n",
    "print(arr)\n",
    "np.split(arr, 5) # 将arr 均分成 5 份\n"
   ],
   "id": "41dc3294724ce461",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 1 9 9 7 9 1 8 9 8]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[array([5, 1], dtype=int32),\n",
       " array([9, 9], dtype=int32),\n",
       " array([7, 9], dtype=int32),\n",
       " array([1, 8], dtype=int32),\n",
       " array([9, 8], dtype=int32)]"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T13:44:55.893240Z",
     "start_time": "2025-07-29T13:44:55.881273Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1, 10, 10)\n",
    "print(arr)\n",
    "np.split(arr, (1, 2, 6)) # 对数组切了3刀，有4分，前3分都是左闭右开"
   ],
   "id": "5df8b67cc5054b7f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 5 9 6 4 1 7 3 5 3]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[array([3], dtype=int32),\n",
       " array([5], dtype=int32),\n",
       " array([9, 6, 4, 1], dtype=int32),\n",
       " array([7, 3, 5, 3], dtype=int32)]"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 107
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T13:49:10.647938Z",
     "start_time": "2025-07-29T13:49:10.626855Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1, 10, 10)\n",
    "print(arr)\n",
    "print(np.reshape(arr, (2, 5)))\n",
    "np.reshape(arr, (1, 2, 5))"
   ],
   "id": "6d18dbcf07b8eef9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 5 6 5 3 9 4 4 1 9]\n",
      "[[2 5 6 5 3]\n",
      " [9 4 4 1 9]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[2, 5, 6, 5, 3],\n",
       "        [9, 4, 4, 1, 9]]], dtype=int32)"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 112
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T13:54:23.821492Z",
     "start_time": "2025-07-29T13:54:23.807290Z"
    }
   },
   "cell_type": "code",
   "source": "np.zeros((1, 10))",
   "id": "b6afe3e625c5e0f4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 115
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "71eed61d625a3a2"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
